
import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

///女性健康
class WomenHealthPage extends StatefulWidget {
  const WomenHealthPage({Key? key}) : super(key: key);

  @override
  State<WomenHealthPage> createState() => _WomenHealthPageState();
}

class _WomenHealthPageState extends State<WomenHealthPage> {

  String content = "";
  DateTime datetime = DateTime.now();
  var periodController = TextEditingController();
  var lengthController = TextEditingController();
  AppAllValueController controller = Get.find();

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg){
      Map map = arg;
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.GetWomenHealth:
        case BleConst.SetWomenHealth:
          controller.showContentDialog(map.toString());
          break;
      }}
    );

  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //导航栏
        title: Text("女性健康".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('读取'.tr),
                  onPressed: (){
                    controller.writeData(BleSDK.readWomenHealth());
                  },
                ),
              )),
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: 10,left: 10,right: 10),
            alignment: Alignment.center,
            child: Text('女性健康信息设置'.tr),
          ),
          Container(
            margin: const EdgeInsets.only(left: 10,right: 10),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text('经期(天)'.tr),
                Expanded(child: TextField(
                  controller: periodController,
                  textAlign: TextAlign.center,
                  decoration: InputDecoration(
                      hintText: '2-7'
                  ),
                  keyboardType: TextInputType.number,
                  textInputAction: TextInputAction.done,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
                  ],
                ))
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 10,right: 10),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text('周期(天)'.tr),
                Expanded(child: TextField(
                  controller: lengthController,
                  textAlign: TextAlign.center,
                  decoration: InputDecoration(
                      hintText: '21-35'
                  ),
                  keyboardType: TextInputType.number,
                  textInputAction: TextInputAction.done,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
                  ],
                ))
              ],
            ),
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('日期选择'.tr),
                  onPressed: (){
                    showDatePicker(context: context, 
                        initialDate: DateTime.now(), 
                        firstDate: DateTime(2000,1,1),
                        lastDate: DateTime(2050,12,31)).then((value){
                       if(value != null){
                         setState(() {
                          datetime = value;
                         });
                       }
                    });
                  },
                ),
              )),
              Expanded(child: Container(
                margin: const EdgeInsets.only(right: 10),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('${datetime.year}-${datetime.month}-${datetime.day}'),
                  ],
                ),
              ))
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('设置'.tr),
                  onPressed: (){
                    if(periodController.text.isEmpty)return;
                    if(lengthController.text.isEmpty)return;
                    int period = int.parse(periodController.text);
                    int length = int.parse(lengthController.text);
                    controller.writeData(BleSDK.setWomenHealth(datetime,period, length));
                  },
                ),
              )),

            ],
          )

        ],
      ),
    );
  }

}
